<template>
  <div class="coupon_container">
    <div class="top_box">
      <a-card style="width: 100%">
        <div>
          <div class="filter_search">
            <div>
              <a-icon type="search" />
              筛选搜索
            </div>
            <div class="btnbox">
              <a-button class="btnLeft"> 重置 </a-button>
              <a-button type="primary"> 查询搜索 </a-button>
            </div>
          </div>
          <div class="coupon_box">
            优惠券名称：<a-input
              class="inputLeft"
              placeholder="优惠券名称"
              style="width: 200px"
            />
            <div>
              优惠券类型：
              <a-select
                style="width: 200px"
                :allow-clear="true"
                @change="handleChange"
              >
                <a-select-option value="全场赠券"> 全场赠券 </a-select-option>
                <a-select-option value="会员赠券"> 会员赠券 </a-select-option>
                <a-select-option value="购物赠券"> 购物赠券 </a-select-option>
                <a-select-option value="注册赠券"> 注册赠券 </a-select-option>
              </a-select>
            </div>
          </div>
        </div>
      </a-card>
    </div>
    <div class="data_box">
      <a-card style="width: 100%">
        <div class="data_list">
          <div>
            <a-icon type="container" />
            数据列表
          </div>
          <div class="btnbox">
            <a-button size="small" @click="$router.push('/addCoupon')">
              添加
            </a-button>
          </div>
        </div>
      </a-card>
    </div>
    <div class="data_table">
      <a-table
        :columns="columns"
        :pagination="pagination"
        :data-source="data"
        bordered
        :row-selection="rowSelection"
      >
        <span slot="id" slot-scope="id">{{ id }}</span>
        <span slot="coupon_name" slot-scope="coupon_name">{{
          coupon_name
        }}</span>
        <span slot="coupon_type" slot-scope="coupon_type">{{
          coupon_type
        }}</span>
        <span slot="coupon_goods" slot-scope="coupon_goods">{{
          coupon_goods
        }}</span>
        <span slot="coupon_threshold" slot-scope="coupon_threshold">{{
          coupon_threshold
        }}</span>
        <span slot="coupon_value" slot-scope="coupon_value">{{
          coupon_value
        }}</span>
        <span slot="applicable_platform" slot-scope="applicable_platform">{{
          applicable_platform
        }}</span>
        <span slot="validity_date" slot-scope="validity_date">{{
          validity_date
        }}</span>
        <span slot="state" slot-scope="state">{{
          state === 0 ? '已过期' : '可使用'
        }}</span>
        <span slot="action">
          <a-button type="link" size="small"> 查看 </a-button>
          <a-button type="link" size="small"> 编辑 </a-button>
          <a-button type="link" size="small"> 删除 </a-button>
        </span>
      </a-table>
    </div>
  </div>
</template>

<script>
const columns = [
  {
    title: '编号',
    dataIndex: 'id',
    width: '5%',
    scopedSlots: { customRender: 'id' },
    align: 'center'
  },
  {
    title: '优惠券名称',
    dataIndex: 'coupon_name',
    width: '14%',
    scopedSlots: { customRender: 'coupon_name' },
    align: 'center'
  },
  {
    title: '优惠券类型',
    dataIndex: 'coupon_type',
    width: '10%',
    scopedSlots: { customRender: 'coupon_type' },
    align: 'center'
  },
  {
    title: '可使用商品',
    dataIndex: 'coupon_goods',
    scopedSlots: { customRender: 'coupon_goods' },
    align: 'center'
  },
  {
    title: '使用门槛',
    dataIndex: 'coupon_threshold',
    scopedSlots: { customRender: 'coupon_threshold' },
    align: 'center'
  },
  {
    title: '面值',
    dataIndex: 'coupon_value',
    width: '5%',
    scopedSlots: { customRender: 'coupon_value' },
    align: 'center'
  },
  {
    title: '适用平台',
    dataIndex: 'applicable_platform',
    scopedSlots: { customRender: 'applicable_platform' },
    align: 'center'
  },
  {
    title: '有效期',
    dataIndex: 'validity_date',
    scopedSlots: { customRender: 'validity_date' },
    align: 'center'
  },
  {
    title: '状态',
    dataIndex: 'state',
    width: '6%',
    scopedSlots: { customRender: 'state' },
    align: 'center'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '15%',
    scopedSlots: { customRender: 'action' },
    align: 'center'
  }
]
const pagination = {
  pageSize: 10
}

import { getCouponList } from '@/api/marketing'
export default {
  data() {
    return {
      data: [],
      columns,
      pagination
    }
  },
  computed: {
    rowSelection() {
      return {
        onChange: (selectedRowKeys, selectedRows) => {
          console.log(
            `selectedRowKeys: ${selectedRowKeys}`,
            'selectedRows: ',
            selectedRows
          )
        },
        getCheckboxProps: (record) => ({
          props: {
            disabled: record.name === 'Disabled User', // Column configuration not to be checked
            name: record.name
          }
        })
      }
    }
  },
  created() {
    this.getCouponList()
  },
  methods: {
    handleChange(value) {
      console.log(`selected ${value}`)
    },
    async getCouponList() {
      const { coupon_item: res } = await getCouponList()
      this.data = res
    }
  }
}
</script>

<style lang="scss">
.coupon_container {
  margin: 20px;

  .top_box,
  .data_box {
    margin-bottom: 20px;
  }

  .filter_search {
    display: flex;
    justify-content: space-between;
  }

  .btnLeft {
    margin-right: 10px;
  }

  .coupon_box {
    margin-left: 20px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;

    .inputLeft {
      margin-right: 40px;
    }
  }
  .data_list {
    display: flex;
    justify-content: space-between;
  }
}
</style>
